package week9;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Index
{
  private String key = null;
  private int recardID = -5;

  public Index()
  {
  }

  public Index(String paramString, int paramInt)
  {
    this.key = paramString;
    this.recardID = paramInt;
  }

  public String getKey()
  {
    return this.key;
  }

  public void setKey(String paramString)
  {
    this.key = paramString;
  }

  public int getRecardID()
  {
    return this.recardID;
  }

  public void setRecardID(int paramInt)
  {
    this.recardID = paramInt;
  }

  public byte[] serialize()
    throws IOException
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream localDataOutputStream = new DataOutputStream(localByteArrayOutputStream);
    localDataOutputStream.writeUTF(this.key);
    localDataOutputStream.writeInt(this.recardID);
    localByteArrayOutputStream.close();
    localDataOutputStream.close();
    return localByteArrayOutputStream.toByteArray();
  }

  public static Index deserialize(byte[] paramArrayOfByte)
    throws IOException
  {
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
    DataInputStream localDataInputStream = new DataInputStream(localByteArrayInputStream);
    Index localIndex = new Index();
    localIndex.setKey(localDataInputStream.readUTF());
    localIndex.setRecardID(localDataInputStream.readInt());
    localByteArrayInputStream.close();
    localDataInputStream.close();
    return localIndex;
  }

  public static boolean matches(byte[] paramArrayOfByte, String paramString, boolean paramBoolean)
    throws IOException
  {
    boolean bool;
    ByteArrayInputStream localByteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
    DataInputStream localDataInputStream = new DataInputStream(localByteArrayInputStream);
    if (paramBoolean)
      bool = localDataInputStream.readUTF().equalsIgnoreCase(paramString);
    else
      bool = localDataInputStream.readUTF().toLowerCase().startsWith(paramString.toLowerCase());
    if (localByteArrayInputStream != null)
      localByteArrayInputStream.close();
    if (localDataInputStream != null)
      localDataInputStream.close();
    return bool;
  }

  public static void sort(Index[] paramArrayOfIndex, boolean paramBoolean)
  {
    int j;
    int l,k;
    int i = paramArrayOfIndex.length - 1;
    Index localIndex = null;
    if (paramBoolean)
    {
      k = 0;
      while (true)
      {
        if (k >= i)
          return;
        l = k;
        for (j = k + 1; j < i + 1; ++j)
          if (paramArrayOfIndex[l].key.toLowerCase().compareTo(paramArrayOfIndex[j].key.toLowerCase()) > 0)
            l = j;
        if (l != k)
        {
          localIndex = paramArrayOfIndex[l];
          paramArrayOfIndex[l] = paramArrayOfIndex[k];
          paramArrayOfIndex[k] = localIndex;
        }
        ++k;
      }
    }
    for (k = 0; k < i; ++k)
    {
      l = k;
      for (j = k + 1; j < i + 1; ++j)
        if (paramArrayOfIndex[l].key.compareTo(paramArrayOfIndex[j].key) < 0)
          l = j;
      if (l != k)
      {
        localIndex = paramArrayOfIndex[l];
        paramArrayOfIndex[l] = paramArrayOfIndex[k];
        paramArrayOfIndex[k] = localIndex;
      }
    }
  }
}